<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: filter.php 502 2014-01-31 15:50:06Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 502 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-31 15:50:06 +0000 (Fri, 31 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$box_smarty = new Smarty();

$box_smarty->assign('tpl_path','templates/'.CURRENT_TEMPLATE.'/');
$box_smarty->assign('html_tpl_path', CURRENT_TEMPLATE.'/html');
$box_smarty->assign('language', $_SESSION['language']);

$filter = array();

require_once(DIR_WS_CLASSES.'class.products_filter.php');
$cls_products_filter = new products_filter();
if(($filter_products_id = $cls_products_filter->get_products_id()) !== false) {
	$filter_products = " and p.products_id in('".implode("', '", $filter_products_id)."') ";
	$cls_products_filter->set_request();
}
else
	$filter_products = "";

if(isset($_GET['cPath'])) {
	$cat_id_temp = explode('_', $_GET['cPath']);
	$cat_id_temp = array_reverse($cat_id_temp);
	$cat_id = $cat_id_temp[0];
} else
	$cat_id = '0';

if(isset($_GET['cPath']) && $category_depth == 'products')
	$box_smarty->assign('FORM_FILTER', draw_form('product_filter', href_link(FILENAME_DEFAULT, 'cPath='.$_GET['cPath']), 'POST'));
else
	$box_smarty->assign('FORM_FILTER', draw_form('product_filter', href_link(FILENAME_PRODUCT_FILTER), 'POST'));

$get_filter = $db->db_query("SELECT filter_id, filter_name, cat_id FROM ".TABLE_PRODUCTS_FILTERS." WHERE parent_id = '0' order by filter_order");

require_once(DIR_FS_INC.'inc.get_subcategories.php');
get_subcategories($sub_categories, $cat_id);
$sub_categories[] = $cat_id;

function countFilter2Cat($filter) {
	global $db, $sub_categories, $filter_products;
	$count = 0;

	for($i = 0; $i < sizeof($sub_categories); $i++) {
		$pf = $db->db_query("
			SELECT
				p2f.products_id
			FROM
				".TABLE_PRODUCTS_TO_FILTERS." p2f,
				".TABLE_PRODUCTS_TO_CATEGORIES." p2c,
				".TABLE_PRODUCTS." p
			WHERE
				p2c.categories_id = '".$sub_categories[$i]."'
				AND p2c.categories_id !='0'
			AND p2f.products_id = p2c.products_id
			AND p.products_status = '1'
				".$filter_products."
			AND p.products_id = p2c.products_id
			AND p2f.filter_id = '".$filter."'
			GROUP BY
				p2f.products_id");
		$count += $pf->_numOfRows;
	}
	return $count;
}

list($req_filter, $req_sub_filter, $req_minp, $req_maxp) = $cls_products_filter->get_request();

if($get_filter->_numOfRows) {
	while(!$get_filter->EOF) {
		$active = false;
		$filter_on = false;
		$active_filter = '';
		$sub = '';
		$total_filter = '';
		
		if(($get_filter->fields['cat_id'] !='' && $get_filter->fields['cat_id'] !='0')) {
			$cat_db = explode('|', $get_filter->fields['cat_id']);
			if(in_array($cat_id, $cat_db) OR $PHP_SELF == 'product_filter.php')
				$active = true;
		}

		//if($active || $get_filter->fields['cat_id'] =='0') {
		if($active) { // just show, if enable cat
			if( !empty($req_sub_filter) && in_array($get_filter->fields['filter_id'], $req_sub_filter)) {
				$active_filter = ' checked=""';
				$sub_class = 'filter_open';
			} 
			else
				$sub_class = 'filter_close';
								
			$sub_filter = $db->db_query("
				SELECT 
					filter_id, filter_name
				FROM
					".TABLE_PRODUCTS_FILTERS." 
				WHERE 
					parent_id = '".$get_filter->fields['filter_id']."'", true);
			if($sub_filter->_numOfRows) {
				
				$counter = '';
				while(!$sub_filter->EOF) {
					$active_sub_filter = '';

					if(!empty($req_sub_filter) && in_array($sub_filter->fields['filter_id'], $req_sub_filter))
						$active_sub_filter = ' checked=""';

					$count = countFilter2Cat($sub_filter->fields['filter_id']);
					if($count > 0) {
					 	$filter_on = true;
					 	$total_filter += $count;
					}
					/*:prjBM: Style Classe für die checkboxen kommt mit disabled nicht klar */
					$sub[] = array(
						'filter_id' => $sub_filter->fields['filter_id'], 
						'name' => $sub_filter->fields['filter_name'],
						'count' => $count, 
						'checked' => $active_sub_filter,
						'disabled' => (1==2 && $count == 0 ? ' disabled=""' : ''),
						'class' => (1==2 && $count == 0 ? ' class="filter_disabled"' : ''),
						'subs' => '');

					$sub_filter->MoveNext();
				}
				if(!empty($req_sub_filter))
					$box_smarty->assign('del_filter', '<input class="css_img_button" type="submit" value="X" title="Filter löschen" name="delete_filter" />');
			}
			
			if(is_array($sub)) {
				$filter[] = array('filter_id' => urldecode($get_filter->fields['filter_id']), 
									'name' => $get_filter->fields['filter_name'],
									'clean_name' => get_url_friendly_text(strtolower(str_replace(' ', '', $get_filter->fields['filter_name']))),
									'checked' => $active_filter,
									'disabled' => ($filter_on == false ? ' disabled=""' : ''),
									'total' => $total_filter,
									'sub_class' => $sub_class,
									'subs' => $sub);
			}
		}					
		$get_filter->MoveNext();
	}
	$box_smarty->assign('filters', $filter);
}
if (count($filter) > 0) {
	$box_smarty->assign('SUBMIT_BUTTON', image_submit('button_send.gif', 'Produkte suchen'));
	$box_smarty->caching = false;
	//echo_e($box_smarty->getTemplateVars(), 'E');
	$box_content = $box_smarty->fetch(CURRENT_TEMPLATE.'/boxes/box_filter.html');
}
?>